package auth.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import auth.service.IAdminService;
import common.dto.admindto.AdminLogin;
import common.model.admin.AdminRole;
import common.model.admin.AdminUser;
import common.model.result.Result;
import common.model.result.ResultCodeEnum;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@RequestMapping("/jiang")
@RestController
public class AdminController {
    
    @Autowired
    private IAdminService adminService;

    //后台登录
    @PostMapping("/login")
    public Result login(@RequestBody AdminLogin adminLogin, HttpServletResponse response) {
        return adminService.login(adminLogin, response);
    }

    //后台登出
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request, HttpServletResponse response) {
        return adminService.logout(request, response);
    }

    //刷新token
    @PostMapping("/refresh-token")
    public Result refreshToken(HttpServletRequest request, HttpServletResponse response) {
        return adminService.refreshToken(request, response);
    }
    
    //重置管理员密码
    @PostMapping("/reset-password")
    public Result resetPassword(@RequestParam Long adminId, @RequestParam String newPassword) {
        return adminService.resetPassword(adminId, newPassword);
    }

    //修复所有管理员密码（仅用于开发环境）
    @PostMapping("/fix-all-passwords")
    public Result fixAllPasswords(@RequestParam String defaultPassword) {
        // 获取所有管理员
        Result result = adminService.findAll();
        if (!result.getSuccess() || result.getData() == null) {
            return Result.failure(ResultCodeEnum.FAIL, "获取管理员列表失败");
        }
        
        List<AdminUser> admins = (List<AdminUser>) result.getData();
        int count = 0;
        
        for (AdminUser admin : admins) {
            // 重置每个管理员的密码
            Result resetResult = adminService.resetPassword(admin.getId(), defaultPassword);
            if (resetResult.getSuccess()) {
                count++;
            }
        }
        
        return Result.success("成功修复 " + count + " 个管理员密码");
    }

    //后台仪表盘
    @GetMapping("/dashboard/cards")
    public Result dashboard() {
        return adminService.dashboard();
    }

    //获取所有管理员
    @GetMapping("/list")
    public Result list() {
        return adminService.findAll();
    }

    //添加管理员
    @PostMapping("/add")
    public Result add(@RequestBody AdminUser admin) {
        return adminService.add(admin);
    }

    //编辑管理员
    @PostMapping("/edit")
    public Result edit(@RequestBody AdminUser admin) {
        return adminService.edit(admin);
    }
    
    //软删除管理员
    @PostMapping("/delete")
    public Result delete(@RequestBody AdminUser admin) {
        return adminService.delete(admin);
    }

    //角色表
    @GetMapping("/roles")
    public Result role() {
        return adminService.role();
    }

    //添加角色
    @PostMapping("/role/add")
    public Result roleAdd(@RequestBody AdminRole adminRole) {
        return adminService.roleAdd(adminRole);
    }

    //获取权限列表
    @GetMapping("/permissions")
    public Result permission() {
        return adminService.permission();
    }

    //获取角色权限列表
    @GetMapping("/role/permissions")
    public Result RolePermission() {
        return adminService.RolePermission();
    }

    //获取在线管理员id
    @GetMapping("/getOnlineAdminId")
    public Result getOnlineAdminId() {
        return adminService.getOnlineAdminId();
    }


}
